package org.example.exception;

import org.example.result.BaseErrorCode;
import org.example.result.StateCode;
import org.springframework.util.StringUtils;
import java.util.Optional;

public abstract class AbstractException extends RuntimeException {

    private final String errorMessage;

    private final String errorCode;

    protected AbstractException() {
        super(BaseErrorCode.SERVICE_ERROR.message());
        this.errorCode = BaseErrorCode.SERVICE_ERROR.code();
        this.errorMessage = BaseErrorCode.SERVICE_ERROR.message();

    }


    protected AbstractException(String errorMessage, String errorCode) {
        super(errorMessage);
        this.errorCode = Optional.ofNullable(StringUtils.hasLength(errorCode) ? errorCode : null).orElse(BaseErrorCode.SERVICE_ERROR.code());
        this.errorMessage = Optional.ofNullable(StringUtils.hasLength(errorMessage) ? errorMessage : null).orElse(BaseErrorCode.SERVICE_ERROR.message());

    }

    protected AbstractException(String errorMessage, Throwable throwable, StateCode stateCode) {
        super(errorMessage, throwable);
        this.errorCode = stateCode.code();
        this.errorMessage = Optional.ofNullable(StringUtils.hasLength(errorMessage) ? errorMessage : null).orElse(stateCode.message());
    }

    protected AbstractException(Throwable throwable) {
        super(throwable.getMessage(), throwable);
        this.errorCode = BaseErrorCode.SERVICE_ERROR.code();
        this.errorMessage = throwable.getMessage();
    }



    public String getErrorMessage() {
        return errorMessage;
    }

    public String getErrorCode() {
        return errorCode;
    }
}
